/**
 * @Auther: MFM
 * @Date: 2021/10/31 - 10 - 31 - 20:19
 * @version: 1.0
 */

public class Bank extends Thread {
    public static class Account {
        private String user;
        private Integer money;

        public Account(String user, Integer money) {
            this.user = user;
            this.money = money;
        }

        public String getUser() {
            return user;
        }

        public void setUser(String user) {
            this.user = user;
        }

        public Integer getMoney() {
            return money;
        }

        public void setMoney(Integer money) {
            this.money = money;
        }
    }

    //要取款的账户 account 要取得金额 money
    private Account account;
    private Integer money;

    public Bank(Account account, Integer money) {
        this.account = account;
        this.money = money;
    }

    @Override
    public void run() {
        synchronized (this.account) {
            if (this.account.getMoney() >= money) {
                System.out.println("取出" + money);
                account.setMoney(account.getMoney()-money);
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            } else {
                System.out.println("取款失败");
            }
        }
    }

    public static void main(String[] args) {
        Account account = new Account("root", 1000);
        new Bank(account,1000).start();
        new Bank(account,1000).start();
    }
}
